package com.bw.module_detail.model.repository

import com.bw.lib_net.BaseResponse
import com.bw.module_detail.model.api.DetailApi
import com.bw.module_detail.model.entity.CommentEntity
import com.bw.module_detail.model.entity.DanMuEntity
import com.bw.module_detail.model.entity.VideoEntity
import com.google.gson.Gson
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody.Companion.toRequestBody
import javax.inject.Inject

/**
 * @author ykj
 * @date 2025-10-22 08:28
 * @description
 */
class DetailRepository @Inject constructor(private val detailApi: DetailApi) {
    /**
     * 获得评论信息
     */
    fun getCommentList(datatype:Int,itemid:String) = flow {
        emit(detailApi.getCommentList(datatype, itemid))
    }.flowOn(Dispatchers.IO)
    /**
     * 发布评论
     */
    fun publishComment(commentEntity: CommentEntity) = flow {
        emit(detailApi.publishComment(commentEntity))
    }.flowOn(Dispatchers.IO)
    /**
     * 获得视频列表
     */
    fun getVideoList(page:Int,pagesize:Int): Flow<BaseResponse<List<VideoEntity>>> = flow {
        emit(detailApi.getVideoList(page, pagesize))
    }.flowOn(Dispatchers.IO)
    /**
     * 获得弹幕信息
     */
    fun getBulletScreenInfo(datatype:Int, itemId:String) = flow {
        emit(detailApi.getBulletScreenInfo(datatype, itemId))
    }.flowOn(Dispatchers.IO)
    /**
     * 发送弹幕
     */
    fun publishBulletScreen(danMuEntity: DanMuEntity) = flow {
        val requestBody = Gson().toJson(danMuEntity).toRequestBody("application/json; charset=utf-8".toMediaTypeOrNull())
        emit(detailApi.publishBulletScreen(requestBody))
    }.flowOn(Dispatchers.IO)
    /**
     * 修改用户金币
     */
    fun modifyCoinAmount(id:String, nm:Int) = flow {
        emit(detailApi.modifyCoinAmount(id, nm))
    }.flowOn(Dispatchers.IO)
}